package imy.virtual.touch.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class IPUtils {

  public static String getLocalIp() {
    String[] arr;
    try {
      Process process = Runtime.getRuntime().exec(
          "adb shell /data/data/com.imy.virtualgame/files/minitouch");
      String data = null;
      BufferedReader ie = new BufferedReader(new InputStreamReader(process.getErrorStream()));
      BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
      String string = in.readLine();
      Log.i("getLocalIp","String:"+string);
      Log.i("getLocalIp","error:"+ ie.readLine());
      arr = string.split("\\s+");
      return arr[2];
    } catch (IOException e) {
      e.printStackTrace();
    }
    return "error";
  }

  public static String intIP2StringIP(int ip) {
    return (ip & 0xFF) + "." +
        ((ip >> 8) & 0xFF) + "." +
        ((ip >> 16) & 0xFF) + "." +
        (ip >> 24 & 0xFF);
  }
}
